/*
 * pack_unpack_test.c
 *
 *  Created on: Jul 15, 2012
 *      Author: Tiberiu S. Petre
 */

void compare_uhfs(unpacked_hf uhf1,unpacked_hf uhf2) {
	int i;
	// Compare LARc
	for(i=0;i<8;i++) {
		if(uhf1.LARc[i]!=uhf2.LARc[i])
			printf("ERROR : LARc[%d] missmatch : %d != %d\n",i,uhf1.LARc[i],uhf2.LARc[i]);
	}
	// Compare RPE/LTP params secondary frame 1
	if(*uhf1.LTP_P1     !=  *uhf2.LTP_P1    ) printf("ERROR : LTP_P1 mismatch : %d != %d\n"     ,*uhf1.LTP_P1,*uhf2.LTP_P1);
	if(*uhf1.LTP_b1     !=  *uhf2.LTP_b1    ) printf("ERROR : LTP_b1 mismatch : %d != %d\n"     ,*uhf1.LTP_b1,*uhf2.LTP_b1);
	if(*uhf1.RPE_M1     !=  *uhf2.RPE_M1    ) printf("ERROR : RPE_M1 mismatch : %d != %d\n"     ,*uhf1.RPE_M1,*uhf2.RPE_M1);
	if(*uhf1.RPE_xmaxc1 !=  *uhf2.RPE_xmaxc1) printf("ERROR : RPE_xmaxc1 mismatch : %d != %d\n" ,*uhf1.RPE_xmaxc1,*uhf2.RPE_xmaxc1);
	for(i=0;i<13;i++)
		if(uhf1.RPE_xMc1[i]   !=  uhf2.RPE_xMc1[i]  )
			printf("ERROR : RPE_xMc1[%d] mismatch : %d != %d\n",i,uhf1.RPE_xMc1[i],uhf2.RPE_xMc1[i]);

	// Compare RPE/LTP params secondary frame 2
	if(*uhf1.LTP_P2     !=  *uhf2.LTP_P2    ) printf("ERROR : LTP_P2 mismatch : %d != %d\n"     ,*uhf1.LTP_P2,*uhf2.LTP_P2);
	if(*uhf1.LTP_b2     !=  *uhf2.LTP_b2    ) printf("ERROR : LTP_b2 mismatch : %d != %d\n"     ,*uhf1.LTP_b2,*uhf2.LTP_b2);
	if(*uhf1.RPE_M2     !=  *uhf2.RPE_M2    ) printf("ERROR : RPE_M2 mismatch : %d != %d\n"     ,*uhf1.RPE_M2,*uhf2.RPE_M2);
	if(*uhf1.RPE_xmaxc2 !=  *uhf2.RPE_xmaxc2) printf("ERROR : RPE_xmaxc2 mismatch : %d != %d\n" ,*uhf1.RPE_xmaxc2,*uhf2.RPE_xmaxc2);
	for(i=0;i<13;i++)
		if(uhf1.RPE_xMc2[i]   !=  uhf2.RPE_xMc2[i]  )
			printf("ERROR : RPE_xMc2[%d] mismatch : %d != %d\n",i,uhf1.RPE_xMc2[i],uhf2.RPE_xMc2[i]);

	// Compare RPE/LTP params secondary frame 3
	if(*uhf1.LTP_P3     !=  *uhf2.LTP_P3    ) printf("ERROR : LTP_P3 mismatch : %d != %d\n"     ,*uhf1.LTP_P3,*uhf2.LTP_P3);
	if(*uhf1.LTP_b3     !=  *uhf2.LTP_b3    ) printf("ERROR : LTP_b3 mismatch : %d != %d\n"     ,*uhf1.LTP_b3,*uhf2.LTP_b3);
	if(*uhf1.RPE_M3     !=  *uhf2.RPE_M3    ) printf("ERROR : RPE_M3 mismatch : %d != %d\n"     ,*uhf1.RPE_M3,*uhf2.RPE_M3);
	if(*uhf1.RPE_xmaxc3 !=  *uhf2.RPE_xmaxc3) printf("ERROR : RPE_xmaxc3 mismatch : %d != %d\n" ,*uhf1.RPE_xmaxc3,*uhf2.RPE_xmaxc3);
	for(i=0;i<13;i++)
		if(uhf1.RPE_xMc3[i]   !=  uhf2.RPE_xMc3[i]  )
			printf("ERROR : RPE_xMc3[%d] mismatch : %d != %d\n",i,uhf1.RPE_xMc3[i],uhf2.RPE_xMc3[i]);

	// Compare RPE/LTP params secondary frame 4
	if(*uhf1.LTP_P4     !=  *uhf2.LTP_P4    ) printf("ERROR : LTP_P4 mismatch : %d != %d\n"     ,*uhf1.LTP_P4,*uhf2.LTP_P4);
	if(*uhf1.LTP_b4     !=  *uhf2.LTP_b4    ) printf("ERROR : LTP_b4 mismatch : %d != %d\n"     ,*uhf1.LTP_b4,*uhf2.LTP_b4);
	if(*uhf1.RPE_M4     !=  *uhf2.RPE_M4    ) printf("ERROR : RPE_M4 mismatch : %d != %d\n"     ,*uhf1.RPE_M4,*uhf2.RPE_M4);
	if(*uhf1.RPE_xmaxc4 !=  *uhf2.RPE_xmaxc4) printf("ERROR : RPE_xmaxc4 mismatch : %d != %d\n" ,*uhf1.RPE_xmaxc4,*uhf2.RPE_xmaxc4);
	for(i=0;i<13;i++)
		if(uhf1.RPE_xMc4[i]   !=  uhf2.RPE_xMc4[i]  )
			printf("ERROR : RPE_xMc4[%d] mismatch : %d != %d\n",i,uhf1.RPE_xMc4[i],uhf2.RPE_xMc4[i]);

}

int16_t in_pcm_frame[160];

int pack_unpack_test() {
	unpacked_hf uhf1, uhf2;
	packed_hf phf;
	int i;
	printf("Generating random samples...\n");
	srandom(time(NULL));
	// Generate random samples
	for (i = 0; i < 160; i++) {
		in_pcm_frame[i] = rand();
		in_pcm_frame[i] >>= 3;
		in_pcm_frame[i] <<= 3;
	}
	printf("Creating unpacked homing frame...\n");
	create_unpacked_hf(uhf1);
	create_unpacked_hf(uhf2);

	printf("Encoding samples...\n");
	rpe_ltp_encode(in_pcm_frame, uhf1);

	printf("Packing uhf1...\n");
	pack_decoder_hf(uhf1,phf);
	printf("Unpacking into uhf2...\n");
	unpack_decoder_hf(phf,uhf2);

	printf("Comparing uhf1 to uhf2...\n");
	compare_uhfs(uhf1,uhf2);

	destroy_unpacked_hf(uhf1);
	destroy_unpacked_hf(uhf2);

	return 0;
}

